﻿using System.Collections.Generic;
using System.Linq;
using Northwind.Data;

namespace Northwind.Service
{
    public class CustomerService : ICustomerService
    {
        private readonly NorthwindEntities _northwindEntities
            = new NorthwindEntities();

        public IList<Customer> GetCustomers()
        {
            return _northwindEntities.Customers
                .Select(
                    c => new Service.Customer
                             {
                                 CustomerID = c.CustomerID,
                                 CompanyName = c.CompanyName
                             }).ToList();
        }

        public Service.Customer GetCustomer(string customerID)
        {
            Data.Customer customer
                = _northwindEntities
                    .Customers.Single(
                        c => c.CustomerID == customerID);
            return new Service.Customer
                       {
                           CustomerID = customer.CustomerID,
                           CompanyName = customer.CompanyName,
                           ContactName = customer.ContactName,
                           Address = customer.Address,
                           City = customer.City,
                           Country = customer.Country,
                           Region = customer.Region,
                           PostalCode = customer.PostalCode,
                           Phone = customer.Phone,
                           Orders 
                            = GetOrders(customer.Orders)
                       };
        }

        private static IEnumerable<Service.Order> GetOrders(
            IEnumerable<Data.Order> order)
        {
            return order.Select(o => new Service.Order
                {
                    OrderID = o.OrderID,
                    OrderDate = o.OrderDate,
                    OrderDetails = GetOrderDetails(o),
                    Freight = o.Freight,
                    ShippedDate = o.ShippedDate
                }).ToList();
        }

        private static IEnumerable<Service.OrderDetail> GetOrderDetails(
            Data.Order order)
        {
            return order.Order_Details.Select(
                o => new Service.OrderDetail
                {
                    Product 
                        = new Service.Product
                        {
                            ProductID 
                                = o.Product.ProductID,
                            ProductName 
                                = o.Product.ProductName
                        },
                    Quantity = o.Quantity,
                    UnitPrice = o.UnitPrice
                }).ToList();
        }

        public void Update(Service.Customer customer)
        {
            Data.Customer customerEntity
                = _northwindEntities
                    .Customers.Single(
                        c => c.CustomerID == customer.CustomerID);
            customerEntity.CompanyName = customer.CompanyName;
            customerEntity.ContactName = customer.ContactName;
            customerEntity.Address = customer.Address;
            customerEntity.City = customer.City;
            customerEntity.Country = customer.Country;
            customerEntity.Region = customer.Region;
            customerEntity.PostalCode = customer.PostalCode;
            customerEntity.Phone = customer.Phone;
            _northwindEntities.SaveChanges();
        }
    }
}